package lc.二叉树;

import utils.Node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/10
 */
public class Math590_N叉树的后序遍历 {

    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        Stack<Node> stack = new Stack<>();

        if(root == null)
            return res;

        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            res.add(node.val);
            for (int i = 0; i < node.children.size(); i++) {
                stack.push(node.children.get(i));
            }
        }
        Collections.reverse(res);
        return res;
    }
}
